package com.example.neusoft.yuejian.Fragment;



import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.neusoft.yuejian.Base.BaseFragment;
import com.example.neusoft.yuejian.R;

import java.util.ArrayList;
import java.util.List;

public class Exercisefragment extends BaseFragment {

    private TabLayout tb_layout;
    private ViewPager vp_main;

    private int tabCount = 3;
    private List<String> tabs;
    private List<Fragment> fragments;

    public static final int MOVABLE_COUNT = 6;

    @Override
    public int getLayoutFile() {
        return R.layout.fragment_exercise;
    }

    @Override
    public void initView() {
       Context context= getActivity();
        tb_layout=view.findViewById(R.id.tb_layout);
        vp_main=view.findViewById(R.id.vp_main);
        initDatas();
        initViewPager();
        initTabLayout();



    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    private void initDatas() {
        fragments = new ArrayList<>();
        tabs = new ArrayList<>();
        tabs.add("菜鸟");
        tabs.add("入门");
        tabs.add("进阶");
        for (int i = 0; i < tabs.size(); i++)
            fragments.add(SonHistFragment.newInstance(tabs.get(i)));
    }

    private void initViewPager() {
        vp_main.setAdapter(new MyPagerAdapter(getActivity().getSupportFragmentManager()));
    }

    private void initTabLayout() {
        //MODE_FIXED标签栏不可滑动，各个标签会平分屏幕的宽度
        tb_layout.setTabMode(tabCount <= MOVABLE_COUNT ? TabLayout.MODE_FIXED : TabLayout.MODE_SCROLLABLE);
        //指示条的颜色
        tb_layout.setSelectedTabIndicatorColor(0xFF46f69d);
        tb_layout.setSelectedTabIndicatorHeight(3);
        //关联tabLayout和ViewPager,两者的选择和滑动状态会相互影响
        tb_layout.setupWithViewPager(vp_main);
        //自定义标签布局
        for (int i = 0; i < tabs.size(); i++) {
            TabLayout.Tab tab = tb_layout.getTabAt(i);
            TextView tv = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.tb_layout, tb_layout, false);
            tv.setGravity(Gravity.CENTER);
            tv.setText(tabs.get(i));
            tab.setCustomView(tv);
        }

    }



    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            bundle.putInt("key", position);
            fragments.get(position).setArguments(bundle);
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}
